package com.jtlrm.ckd.mvp.view.adapter;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.sdk.util.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.entity.YinShiDetailAdpterEntity;
import com.jtlrm.ckd.entity.YinShiDetailEntity;
import com.jtlrm.ckd.mvp.view.activity.ShiWuSelectActivity;
import com.jtlrm.ckd.mvp.view.activity.YinShiTuPianActivity;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.ArrayList;

public class YinShiDetailAdapter extends BaseQuickAdapter<YinShiDetailAdpterEntity, BaseViewHolder> {


    private boolean isEdit = false;
    PicClickListener picClickListener;

    public YinShiDetailAdapter() {
        super(R.layout.yin_shi_detail_item);
    }


    @Override
    protected void convert(final BaseViewHolder helper, final YinShiDetailAdpterEntity item) {
        if (isEdit) {
            helper.setText(R.id.add_yong_can_title, "新增" + item.getYongCanType().getHaveMealsTypeName());
            helper.getView(R.id.add_yong_can).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.add_yong_can).setVisibility(View.GONE);
        }
        helper.setText(R.id.title, item.getYongCanType().getHaveMealsTypeName());
        RecyclerView subView = helper.getView(R.id.recyclerView);
        subView.setLayoutManager(new LinearLayoutManager(mContext));
        subView.addItemDecoration(createItemDecoration());
        YinShiDetailSubAdapter adapter = new YinShiDetailSubAdapter(isEdit);
        subView.setAdapter(adapter);
        adapter.replaceData(item.getYinShiDetailEntities());
        helper.getView(R.id.add_yong_can).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShiWuSelectActivity.goSelect(mContext, item);
            }
        });
        helper.getView(R.id.picture_browse).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (picClickListener != null) {
                    if (item.isHasTuPian()) {
                        picClickListener.clickPic(item.getYongCanType().getId());
                    } else {
                        ToastUtil.TextToast(mContext, "暂无图片");
                    }
                }

            }
        });
        TextView browesT = helper.getView(R.id.picture_browse);
        ImageView browsI = helper.getView(R.id.picture_icon);
        if (item.isHasTuPian()) {
            browesT.setTextColor(mContext.getResources().getColor(R.color.color_9A9A9A));
            browsI.setImageResource(R.drawable.picture_browse);
        } else {
            browesT.setTextColor(mContext.getResources().getColor(R.color.grey_400));
            browsI.setImageResource(R.drawable.picture_browse_grey);
        }
    }

    public void setPicClickListener(PicClickListener picClickListener) {
        this.picClickListener = picClickListener;
    }

    public interface PicClickListener {
        void clickPic(String yongCanTypeId);
    }

    protected RecyclerView.ItemDecoration createItemDecoration() {
        return new DefaultItemDecoration(ContextCompat.getColor(mContext, R.color.line_color), 2, 1, -1);
    }

    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
        notifyDataSetChanged();
    }
}

